/**
 * 启动分享页面
 */
import Util from "../../../utils/utils";
import User from "../../../utils/user";
Page({
  shareConfig:{},
  isShareLoading:false,
  data:{
    initUser:true,
    openLogin:false,
    ShowShareBox:false,
    ShowQrcodeBox:false,
    userInfo:{},
    qrcode_url:'',
  },
  /**
   * 页面载入完成
   */
  initPage: function(options){
    let self  = this;
    let app   = getApp();
    /**
     * 传入参数定义
     * {
     *   type:'all',
     *   url:'分享地址',
     *   title:'标题',
     *   desc:'描述',
     *   image:'图片地址',
     * }
     * @type {number}
     */
    console.log(options['data']);
    let json  = options.hasOwnProperty('data') ? JSON.parse(decodeURIComponent(options['data'])) : {};

    let type   = json.hasOwnProperty('type') ? json['type'] : 'all';
    let url    = json.hasOwnProperty('url') ? json['url'] : '';
    let title  = json.hasOwnProperty('title') ? json['title'] : '';
    let desc   = json.hasOwnProperty('desc') ? json['desc'] : '';
    let image  = json.hasOwnProperty('image') ? json['image'] : '';
    let qrcode = json.hasOwnProperty('qrcode') ? json['qrcode'] : '';
    let auth   = json.hasOwnProperty('auth') ? Util.fixInt(json['auth']) : 0;

    if(url === ''){
      url = '/pages/home/index';
    }
    if(url.indexOf('http') > -1){
      url = '/pages/index/router/index?type=2&url=' + encodeURIComponent(url) + '&title=' + encodeURIComponent(title) + '&auth=' + auth;
    }

    self.setData({
      qrcode_url: qrcode
    });
    self.shareConfig = {
      url:url,
      title:title,
      desc:desc,
      image:image,
    };

    /**
     * 如果要求登录的处理
     */
    if(auth === 1){
      self.getUserInfo(function (userInfo) {
        let token = Util.token;
        self.setData({
          ShowShareBox:true
        });
      },function () {
        self.setData({
          initUser:false,
          openLogin:true
        });
      });
    }else{
      self.setData({
        ShowShareBox:true
      });
    }
  },
  scanShare:function(){
    this.setData({
      ShowQrcodeBox:true
    });
  },
  closePopup:function (e) {
    let type = e.currentTarget.dataset.type;
    let setD = {};
    if(this.data.hasOwnProperty(type)){
      setD[type] = false;
      this.setData(setD);
      if(type === 'ShowShareBox'){
        //关闭弹窗时，处理跳转事件
        wx.navigateBack();
      }
    }
  },
  onShow:function(){
    if(this.isShareLoading === true){
      this.isShareLoading = false;
      wx.navigateBack();
    }
  },
  onShareAppMessage:function (eve) {
    let _param,pages,currentPage;
    let _share  = this.shareConfig;
    let channel = User.getShareParam(0);
    if(!_share.title){
      _share['title'] = '快来领取优惠券吧';
    }
    if(!_share.url){
      pages = getCurrentPages();
      currentPage = pages[pages.length - 1];
      _share['url'] = currentPage.route;
    }
    if(_share['url'].indexOf('?') === -1){
      _share['url'] += '?v=' + Util.version;
    }
    if(channel.hasOwnProperty('ekey') && channel.ekey && _share['url'].indexOf('ekey') === -1){
      _share['url'] += '&ekey=' + channel['ekey'];
    }
    if(channel.hasOwnProperty('shareUid') && channel.shareUid > 0 && _share['url'].indexOf('uid') === -1){
      _share['url'] += '&uid=' + channel['shareUid'];
    }
    _param = {
      title:_share['title'],
      path:_share['url']
    };
    if(_share['image']){
      _param['imageUrl'] = _share['image'];
    }
    this.isShareLoading = true;
    Util.log('分享参数',_param,'channel:',channel);
    return _param;
  }
});
